package com.enjoybyte.pangu.controller;

import com.enjoybyte.pangu.component.minio.util.MinioUtil;
import com.enjoybyte.pangu.util.Result;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController
@RequestMapping("/file")
public class FileController {
    @Resource
    private MinioUtil minioUtil;

    /**
     * 上传文件
     */
    @PostMapping(value = "/upload")
    public Result uploadReport(MultipartFile file) {
        // 获取到上传的文件名
        String fileName = file.getOriginalFilename();
        // 上传文件
        minioUtil.upload(file, fileName);
        // 获取上传的文件地址
        return Result.ok(minioUtil.getFileUrl(fileName));
    }

    /**
     * 预览文件
     */
    @GetMapping("/preview")
    public Result preview(String fileName) {
        return Result.ok(minioUtil.getFileUrl(fileName));
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    public void download(String fileName, HttpServletResponse response) {
        minioUtil.download(response, fileName);
    }

    /**
     * 删除文件
     */
    @GetMapping("/delete")
    public Result delete(String fileName) {
        minioUtil.delete(fileName);
        return Result.ok();
    }
}
